package qlsl.androiddesign.http.service.subservice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import qlsl.androiddesign.constant.MessageConstant;
import qlsl.androiddesign.constant.WhatConstant;
import qlsl.androiddesign.entity.otherentity.Entity;
import qlsl.androiddesign.entity.otherentity.ShoppingDetail;
import qlsl.androiddesign.handler.subhandler.HttpHandler;
import qlsl.androiddesign.http.HttpListener;
import qlsl.androiddesign.http.service.baseservice.BaseService;
import qlsl.androiddesign.http.xutils.HttpProtocol;
import qlsl.androiddesign.thread.HandlerThread;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 查看商品模块 <br/>
 * 
 */
public class ShoppingDetailService extends BaseService {

	private static String className = getClassName(ShoppingDetailService.class);

	
	public static void queryShoppingDetai(final String goodsinfoid, final FunctionView<?> functionView, final HttpListener listener) {
		final HttpHandler handler = getHandler(functionView, listener, className, "queryShoppingDetai");
		new HandlerThread(className, "queryShoppingDetai") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getgoodsinfobyid").addParam("goodsinfoid", goodsinfoid).post();
					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}
					
					ShoppingDetail object = JSON.toJavaObject(jo, ShoppingDetail.class);

					handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS, object);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}
	

	
	public static void createCollectgoods(final String goodsinfoid, final FunctionView<?> functionView, final HttpListener listener) {
		final HttpHandler handler = getHandler(functionView, listener, className, "createCollectgoods");
		new HandlerThread(className, "createCollectgoods") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("collectgoods").addParam("goodsinfoid", goodsinfoid).post();
					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}
					
//					Entity object = JSON.toJavaObject(jo, Entity.class);

					handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS, MessageConstant.MSG_EMPTY);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}
	/**
	 * 加入购物车
	 * @param goodsinfoid
	 * @param functionView
	 * @param listener
	 */
	public static void insertShoppingCart(final String nums,final String goodsinfoid, final FunctionView<?> functionView, final HttpListener listener) {
		final HttpHandler handler = getHandler(functionView, listener, className, "insertShoppingCart");
		new HandlerThread(className, "insertShoppingCart") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("addshoppingcart").addParam("goodsinfoid", goodsinfoid)
							.addParam("nums", nums).post();
					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}
					
//					Entity object = JSON.toJavaObject(jo, Entity.class);

					handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS, MessageConstant.MSG_EMPTY);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}
}
